<template>
  <div>
    <list id-key="id" ref="list" unit="角色" :disabled="disabled" :show-query="showQuery" :show-checkbox="showCheckbox"
      show-index :query-params="queryParams" page @get-data="onGetData" custom-get-data :data="data" :total="total">
      <template #toolbar-left>
        <slot name="toolbar-left"></slot>
      </template>
      <template #query>
        <el-form-item label="角色名称">
          <el-input v-model.trim="queryParams.name" clearable></el-input>
        </el-form-item>
      </template>
      <template #column>
        <el-table-column prop="name" label="角色名称" sortable="custom" width="150"></el-table-column>
        <el-table-column prop="remarks" label="角色备注" min-width="150"></el-table-column>
        <el-table-column prop="createTime" width="130" label="创建时间" sortable="custom">
          <template slot-scope="{ row }">
            <span>{{ row.createTime | toShortDatetimeString }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label="最近更新时间" width="130" sortable="custom">
          <template slot-scope="{row}">
            <span>{{ row.updateTime | toShortDatetimeString }}</span>
          </template>
        </el-table-column>
        <el-table-column width="100" label="菜单权限" align="center">
          <template slot-scope="{row}">
            <span class="c-link" @click="showMenuPermissionDialog(row.id)">查看</span>
          </template>
        </el-table-column>
        <!-- slot[column] -->
        <slot name="column"></slot>
        <!--/ slot[column]-->
      </template>
    </list>
    <menu-permission-dialog ref="menuPermissionDialog" :in-dialog="inDialog" />
  </div>
</template>
<script>
import list from "@/components/common/list";
import menuPermissionDialog from "./menuPermissionDialog";
import api from "@/api/sys/role";

export default {
  components: {
    list,
    menuPermissionDialog,
  },
  props: {
    disabled: {
      type: Boolean,
      default: false
    },
    showQuery: {
      type: Boolean,
      default: true
    },
    showCheckbox: {
      type: Boolean,
      default: false
    },
    inDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      api,
      //查询条件字段
      queryParams: {
        name: "",
      },
      data: [],
      total: 0
    };
  },
  methods: {
    async showMenuPermissionDialog(roleId) {
      await this.$refs.menuPermissionDialog.showByRoleId(roleId);
    },
    onGetData(params) {
      this.$refs.list.loading(true);
      api.getPageList(params).then(res => {
        this.data = res.data.list;
        this.total = res.data.total;
        this.$refs.list.loading(false);
      })
    },
    reload() {
      this.$refs.list.reload();
    },
    async deleteById(id) {
      await this.$confirm("您确定要删除此角色吗？", "删除确认", {
        type: "warning",
      });
      this.$refs.list.loading();
      let res = await api.deleteById(id);
      if (res.code != 0) {
        return;
      }
      this.reload();
    }
  },
};
</script>